package org.monkey4j;

import java.util.Collection;

import org.apache.commons.beanutils.PropertyUtils;
import org.junit.runners.model.Statement;

import com.google.common.base.Supplier;

/**
 * {@link Statement} implementation used for repetative execution in case of a suppliers amount of test data.
 * 
 * @author hillger.t
 */
class RepetitiveStatement extends Statement {
	private final Supplier<? extends Collection<?>> supplier;
	private final Statement statement;
	private final Holder holder;

	public RepetitiveStatement(Statement statement, Holder holder, Supplier<? extends Collection<?>> supplier) {
		this.statement = statement;
		this.supplier = supplier;
		this.holder = holder;
	}

	@Override
	public void evaluate() throws Throwable {
		for (Object currentValue : this.supplier.get()) {
			PropertyUtils.setProperty(this.holder.getBean(), this.holder.getProperty(), currentValue);

			this.statement.evaluate();
		}
	}
}
